package org.itboys.weixin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.collection.CommonCollectionUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.weixin.entity.WeixinRel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class WeixinRelService extends BaseWeixinService<WeixinRel, Long> {

	private static final long serialVersionUID = 3064786257837853564L;
	
	@Resource(name="weixinDS")
	private MongoDataSource keywordDataSource;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return keywordDataSource;
	}

	@Override
	protected Class<WeixinRel> getEntityClass() {
		return WeixinRel.class;
	}
	
	public void save(WeixinRel detail){
		detail.setCreateTime(new Date());
		detail.setCreator(AdminSessionHolder.getAdminUserId());
		super.save(detail);
	}
	
	public void update(WeixinRel detail){
		WeixinRel detail2 = getById(detail.getId());
		detail.setCreateTime(detail2.getCreateTime());
		detail.setCreator(detail2.getCreator());
		detail.setUpdater(AdminSessionHolder.getAdminUserId());
		detail.setUpdateTime(new Date());
		super.update(detail);
	}
	
	//用户微信关联
	@Transactional
	public void insertRel(final Long relObjectId,List<Long> weixinIds,final String type){
		//取消用户原来的角色
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(1);
		param.put("relObjectId", relObjectId);
		List<WeixinRel> list = list(param);
		for(WeixinRel weixinRel : list){
			super.doDelete(weixinRel.getId());
		}
		
		if(relObjectId==null || CommonCollectionUtils.isEmpty(weixinIds)){
			return ;
		}
		
		List<WeixinRel> weixinRels = Lists.transform(weixinIds, new Function<Long, WeixinRel>() {
			@Override
			public WeixinRel apply(Long weixinId) {
				WeixinRel weixinRel = new WeixinRel();
				weixinRel.setWeixinId(weixinId);
				weixinRel.setRelObjectId(relObjectId);
				weixinRel.setRelType(type);//关联微信
				AdminSessionHolder.prepareAdminLoginData(weixinRel);
				return weixinRel;
			}
		});
		for(WeixinRel rel : weixinRels){
			save(rel);
		}
	}
	
	//查询微信表   根据  where rel_object_id=userId and rel_type=1 and is_deleted
	/**
	 * 根据条件查询
	 * @return
	 */
	
	public List<Long>  list(Long userId,String type){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(1);
		param.put("relObjectId", userId);
		param.put("relType", type);
		List<WeixinRel> list = super.list(param);
		List<Long> ids = new ArrayList<Long>();
		for(WeixinRel rel : list){
			ids.add(rel.getWeixinId());
		}
		return ids;
		
	}
	
	@Transactional
	public int deleteByObjIdAndType(Long relObjectId,String relType){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(1);
		param.put("relObjectId", relObjectId);
		List<WeixinRel> list = list(param);
		for(WeixinRel weixinRel : list){
			super.delete(weixinRel.getId());
		}
		return 1;
	}
	
}
